import { aftermarketDetails, aftermarketCancel, aftermarketSureFare, aftermarketSurenoFare, aftermarketComplete, getKuaidiInfo, setAftermarket, aftermarketSetDesc } from '@/api/aftermarket'
import checkDialog from './checkDialog.vue'
import commitDialog from './commitDialog.vue'
import refundDialog from './refundDialog.vue'
export default {
  components: {
    checkDialog,
    commitDialog,
    refundDialog
  },
  props: ['editdata', 'statusOptions', 'typeOptions'],
  data() {
    return {
      aftermarketid: null,
      desc: null,
      status: 1,
      details: null, // 售后详情
      detailType: ['新增描述', '添加收发信息', '填写收货信息', '取消售后申请', '客服审核', '客服提交售后物流', '客服取消售后申请', '客服售后完成','客服需寄回','客服无需寄回','客服回话'],
      reasonTxt: ['不符合售后范围', '退换货商品影响二次销售', '其他'],
      dialogCheckVisible: false,
      dialogCommitVisible: false,
      dialogRefoundVisible: false,
      dialogFareVisible: false,
      cancelVisible: false,
      sureFareVisible: false,
      surenoFareVisible: false,
      completeVisible: false,
      showPopover: false, // 快递详细弹窗
      deliveryList: [], // 快递详细信息
      tableDataNew: [],
      tableData: [{
        date: '2016-05-02',
        name: '王小虎',
        address: '上海市普陀区金沙江路 1518 弄'
      }, {
        date: '2016-05-04',
        name: '王小虎',
        address: '上海市普陀区金沙江路 1517 弄'
      }, {
        date: '2016-05-01',
        name: '王小虎',
        address: '上海市普陀区金沙江路 1519 弄'
      }, {
        date: '2016-05-03',
        name: '王小虎',
        address: '上海市普陀区金沙江路 1516 弄'
      }]
    }
  },
  computed: {
    aftermarketType() {
      const obj = this.typeOptions.find(item => this.details && item.value === +this.details.type)
      return obj ? obj.label : null
    }
  },
  created() {
    console.log('editdata', this.editdata)
    if (this.editdata) {
      this.aftermarketid = this.editdata.aftermarketid
      this.getData()
    }
  },
  methods: {
    closePage() {
      this.$router.push({ query: {}}) // 清空路由query参数
      // this.$router.push({ path: '/aftermarket/aftermarket', query: {}})
      this.$parent.closeEditpage(true)
      // this.$parent.getData(1);
      // this.resetFile()
      // this.getData()
    },
    getData() {
      aftermarketDetails({ aftermarketid: this.aftermarketid }).then(res => {
        console.log('售后详情', res)
        // 批量添加details新属性并触发视图更新
        this.details = Object.assign({}, this.details, res.detail)
        this.status = this.details.status - 0 // 需要转化为数字格式
        // 加上物流弹窗
        const detail = this.details.detail
        if (detail[0]) {
          detail.forEach(item => {
            // type === 6 才会有物流信息
            if (item.type === '6') {
              this.$set(item, 'showPopover', false)
            }
          })
        }
		    this.tableDataNew = res.detail.repeat  ? res.detail.repeat : []
      })
    },
    // 下拉改变售后状态
    async changeStatus(status) {
      const res = await setAftermarket({ aftermarketid: this.aftermarketid, status })
      this.$notify({
        title: '提示',
        type: res.code === 1001 ? 'success' : 'error',
        message: res.msg,
        position: 'bottom-right'
      })
    },

    // 需寄回
    async handleSureFare() {
      this.sureFareVisible = false
      const res = await aftermarketSureFare({ aftermarketid: this.aftermarketid })
      this.$notify({
        title: '提示',
        type: res.code === 1001 ? 'success' : 'error',
        message: res.msg,
        position: 'bottom-right'
      })
      // this.getData()
      // this.$emit('getData')
      this.closePage()
    },

    // 无需寄回
    async handleSurenoFare() {
      this.surenoFareVisible = false
      const res = await aftermarketSurenoFare({ aftermarketid: this.aftermarketid })
      this.$notify({
        title: '提示',
        type: res.code === 1001 ? 'success' : 'error',
        message: res.msg,
        position: 'bottom-right'
      })
      this.closePage()
    },

    // 取消售后申请
    async handleCancel() {
      this.cancelVisible = false
      const res = await aftermarketCancel({ aftermarketid: this.aftermarketid })
      this.$notify({
        title: '提示',
        type: res.code === 1001 ? 'success' : 'error',
        message: res.msg,
        position: 'bottom-right'
      })
      this.closePage()
    },
    // 售后完成
    async handleComplete() {
      this.completeVisible = false
      const res = await aftermarketComplete({ aftermarketid: this.aftermarketid })
      this.$notify({
        title: '提示',
        type: res.code === 1001 ? 'success' : 'error',
        message: res.msg,
        position: 'bottom-right'
      })
      this.closePage()
    },
    // 获取快递详细信息
    async getKuaidiInfo(item) {
      if (!item) return
      this.deliveryList = []
      // 先关闭其它
      const detail = this.details.detail
      if (detail[0]) {
        detail.forEach(item => {
          // type === 6 才会有物流信息
          if (item.type === '6') {
            this.$set(item, 'showPopover', false)
          }
        })
      }
      this.$set(item, 'showPopover', true)
      const ordernum = item.kefufareorder || item.usersfareorder
      const comcode = item.kefucomcode || item.userscomcode
      const res = await getKuaidiInfo({ ordernum, comcode })
      this.deliveryList = res.detail.status === 1 ? res.detail.content : []
      console.log('res', res)
    },

    // 售后反馈提交
    async aftermarketFeedbackSubmit() {
      console.log(this.desc + ':::' + this.aftermarketid)
      if (!this.desc) {
        this.$message({
          message: '留言不能为空!',
          type: 'warning'
        })
        return
      }
      const res = await aftermarketSetDesc({ aftermarketid: this.aftermarketid, desc: this.desc })
      console.log(res)
      if (res.code === 1001) {
        this.$message({
          message: '恭喜你，提交成功',
          type: 'success'
        })
      }
	  this.closePage()
    }
  }
}
